package com.yasenagat

/**
 * Created by yasenagat on 17/5/29 Time 下午3:41.
 */

/**
 * 普通函数
 */
fun sum(a: Int, b: Int): Int {
    return a + b;
}

/**
 * 函数体为表达式，返回值为不用指定类型，为推断类型
 */
fun sum2(a: Int, b: Int) = a + b * 2

/**
 * 无返回值
 */
fun printSum(a: Int, b: Int): Unit {
    println("$a+$b=${a + b}")
}

/**
 * 无返回值，直接省略返回值
 */
fun noReturnValue(a: Int) {
    println("$a*2=${a * 2}")
}

fun main(args: Array<String>) {

    var result = sum(1, 2);
    println(result)

    var a = 2
    var b = 3
    var c: Int = 5
    var d: Int
    d = 1

    println("$a+$b*2=${sum2(a, b)}")
    printSum(d, c)

    noReturnValue(c)

    var number = 5
    number++
    println(number)
    number += 1
    println(number)

    //字符串模板和表达式
    val str = "hello"
    var s = "$str world ${str.replace("l", "##", false)}"
    println(s)
    s = s.replace("l", "##", false)
    println(s)

    //逻辑判断if

    println("max(3,4) = ${max(3, 4)}")
    println("min(5,9) = ${min(5, 9)}")

    //可以设置为null
    var str1: String? = "123"
    println(str1)
    str1 = null
    println(str1)
    ////不可以设置为null
    var str2: String = "abc"
    //不可以设置为null
//    str2 = null


    //类型检查和自动转换

    println(checkType(1))
    println(checkType(true))
    println(checkType("kotlin"))
    println(checkType(1.3333))

    var ten: Double = 10.00
    var three: Double = 3.00
    println(ten / three)

    fun test() {
        println("I am test method")
    }

    test()

    //for循环
    val nn = listOf(1, 23, 4, 5)

    for (n in nn) {
        println("index=${nn.indexOf(n)} ; value=$n")
    }

    val mm = listOf("a", "b", "d")
    //mm.indices = 0..2 ### mm.size=3
    for (index in mm.indices) {
        println("index=$index value=${mm.get(index)}")
    }

    //while
    var i = 0;
    while (i < nn.size) {
        println(nn.get(i))
        i++
    }

    //when
    fun w(i: Int) {
        when (i) {
            1 -> println("a")
            2 -> println("b")
            3 -> println("c")
            4 -> println("d")
            5 -> test()
        }
    }

    w(2)
    w(5)

    //ranges 范围
    for (x in 0..10) {
        println(x)
    }

    //是否在某个范围内
    val x = 10
    val y = 9
    if (x in 1..y + 1) {
        println("fits in range")
    }

    if (y !in 0..5) {
        println("not in range")
    }

    //步长增加
    for (x in 1..10 step 2) {
        print(x)
    }
    println()

    //步长减少
    for (x in 9 downTo 0 step 4) {
        print(x)
    }
    println()

    //set集合,把重复的去掉
    val items = setOf("apple", "banana", "kiwi", "kiwi")
    println(items.size)
    println(items.indices)
    //when只执行第一个符合条件的
    when {
        "orange" in items -> println("juicy")
        "kiwi" in items -> println("kotlin")
        "apple" in items -> println("apple is fine too")
    }

    items.filter { it.contains("a") }.sortedBy { it }.map { it.toUpperCase() }.forEach { println(it) }

}

fun checkType(obj: Any): Int? {

    if (obj !is String) {
        return null
    }

    //自动转换
    if (obj is String && obj.length > 0) {
        return obj.length
    }

    return null
}

fun max(a: Int, b: Int): Int {
    if (a > b) {
        return a
    } else {
        return b
    }
}

fun min(a: Int, b: Int) = if (a > b) b else a

//是否允许返回null
fun parseInt(a: String): Int? {
    return a.toInt()
}
